<template>
  <div>
    <div class="doumee-table-buttons">
      <el-button type="primary" @click="toAdd(0)">{{
        translate("add")
      }}</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border fit :header-row-class-name="'table-header'" class="doumee-element-table">
      <el-table-column align="center" :label="translate('systemFirm')" prop="manufacturerName" min-width="140" show-overflow-tooltip />
      <el-table-column align="center" :label="translate('interfaceAddr')" prop="url" min-width="140" show-overflow-tooltip />
      <el-table-column align="center" :label="translate('loginAccount')" prop="account" min-width="120" show-overflow-tooltip />
      <el-table-column align="center" :label="translate('applyVenue')" prop="venueName" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{
            scope.row.venueType === "0"
              ? translate("allVenues")
              : scope.row.venueName
          }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="translate('createTime')" prop="createTime" min-width="160" show-overflow-tooltip />
      <el-table-column align="center" :label="translate('status')" min-width="100" fixed="right">
        <template slot-scope="scope">
          <!-- :disabled="meta.indexOf('ComboStatus') == -1" -->
          <el-switch v-model="scope.row.status" active-value="1" inactive-value="0" @change="statusAction(scope.row.id, scope.row.status)" />
        </template>
      </el-table-column>
      <el-table-column :label="translate('handle')" min-width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button type="text" @click="toAdd('1', scope.row.id)">{{
              translate("edit")
            }}</el-button>
            <el-button type="text" class="red" @click="toDelete(scope.row.id)">{{ translate("del") }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Pagination :total="total" :page="pagination.page" @current-change="getList" @SizeChange="handleSizeChange" />
    <!-- 新增 编辑 -->
    <el-dialog :close-on-click-modal="modalDialog" :lose-on-press-escape="modalDialog" :title="
        addEditType ? translate('editParkConfig') : translate('addParkConfig')
      " :visible.sync="shopDia" width="600px" @closed="cancelDia">
      <el-form ref="ruleForm" :model="addForm" label-width="auto" :rules="rules">
        <el-form-item :label="translate('systemFirm')" prop="manufacturer">
          <el-select v-model="addForm.manufacturer" :placeholder="translate('systemFirmPlace')" style="width: 100%">
            <el-option v-for="item in manufacturerList" :key="item.id" :label="item.manufacturer" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item :label="translate('interfaceAddr')" prop="url">
          <el-input v-model="addForm.url" :placeholder="translate('qsr')" maxlength="200" />
        </el-form-item>
        <el-form-item :label="translate('loginAccount')" prop="account">
          <el-input v-model="addForm.account" :placeholder="translate('qsr')" maxlength="50" />
        </el-form-item>
        <el-form-item :label="translate('loginPsd')" prop="pwd">
          <el-input v-model="addForm.pwd" :placeholder="translate('qsr')" autocomplete="new-password" type="password" maxlength="50" />
        </el-form-item>
        <el-form-item :label="translate('applyVenue')" prop="venueType">
          <el-radio-group v-model="addForm.venueType">
            <el-radio label="0">{{ translate("allVenueAble") }}</el-radio>
            <el-radio label="1">{{ translate("selVenueAble") }}</el-radio>
          </el-radio-group>
          <div v-if="addForm.venueType == 1">
            <el-checkbox-group v-model="addForm.venueList">
              <el-checkbox v-for="item in relation" :key="item.id" :label="item.id" :name="item.id">{{ item.name }}</el-checkbox>
            </el-checkbox-group>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button v-preventReClick @click="cancelDia">{{
          translate("cancel")
        }}</el-button>
        <el-button :loading="publishLoading" type="primary" @click="confirmSub">{{ translate("enter") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pdConAdd, pdConDel, pdConDisable, pdConEdit, venueList, pdConEditDetail, pdConList, manufacturerList } from '@/api'
import { parseTime } from '@/utils'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'NoticeVenue',
  components: {
    Pagination
  },
  data () {
    return {
      queryFormConfig,
      modalDialog: this.$store.state.app.dialog,
      publishLoading: false,
      listLoading: false,
      list: [],
      meta: [],
      manufacturerList: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {},
      relation: [],
      addEditType: 0,
      shopDia: false,
      addForm: {
        venueType: '0',
        venueList: []
      },
      rules: {
        account: [
          { required: true, message: this.translate('loginAccountPlace'), trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: this.translate('loginPsdPlace'), trigger: 'blur' }
        ],
        url: [
          { required: true, message: this.translate('interfaceAddrP'), trigger: 'blur' }
        ],
        manufacturer: [
          { required: true, message: this.translate('systemFirmPlace'), trigger: 'change' }
        ]
      }
    }
  },
  created () {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
    this.getManufacturer()
    this.getList(1)
  },
  mounted () {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated () {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    loadingTable () {
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },
    getManufacturer () {
      manufacturerList({ param: { type: 9 } })
        .then((res) => {
          if (res.errorCode === '000000') {
            this.manufacturerList = res.recordList || []
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    statusAction (id, status) {
      pdConDisable({ param: { id, status } }).then(res => {
        this.getList()
        if (res.errorCode === '000000') {
          this.$message.success(status === 0 || status === '0' ? this.translate('jyScs') : this.translate('qyScs'))
        }
      }, () => {
        this.getList()
      })
    },
    // 获取场馆列表
    getVenueList () {
      venueList({
        param: {}
      }).then(res => {
        if (res.errorCode !== '000000') return
        this.relation = res.recordList
        const arr = res.recordList
        this.queryFormConfig.formItems[0].options = arr
      })
    },
    handleSizeChange (value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList (page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      this.listLoading = true
      pdConList({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
          if (res.totalCount && res.recordList.length === 0 && pagination.page > 1) {
            this.getList(Math.ceil(res.totalCount / pagination.rows))
          }
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    // 清空筛选项
    clearFilters () {
      this.filters = {}
      this.getList(1)
    },
    // 新增/编辑消息
    toAdd (type, id) {
      this.addEditType = type
      if (id) {
        pdConEditDetail({ param: { id } }).then(res => {
          if (res.errorCode === '000000') {
            const obj = res.record
            if (obj.venueIds) {
              obj.venueList = obj.venueIds.split(',')
            } else {
              obj.venueList = []
            }
            this.addForm = obj
            this.shopDia = true
            this.$nextTick(() => {
              this.$refs.ruleForm.clearValidate()
            })
          }
        })
      } else {
        this.addForm = {
          venueType: '0',
          venueList: []
        }
        this.shopDia = true
        this.$nextTick(() => {
          this.$refs.ruleForm.clearValidate()
        })
      }
    },
    // 取消新增/编辑
    cancelDia () {
      this.shopDia = false
    },
    // 确认新增/编辑
    confirmSub () {
      const that = this
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const { addEditType } = this
          const param = { ...this.addForm }
          if (param.venueType && param.venueType === '1') {
            if (!param.venueList || param.venueList.length === 0) {
              return this.$message.warning(this.translate('selVenuesPlace'))
            }
          }
          param.venueIds = param.venueList.join(',')
          that.publishLoading = true
          if (addEditType) {
            pdConEdit({ param }).then(res => {
              if (res.errorCode !== '000000') return
              that.publishLoading = false
              that.shopDia = false
              that.getList()
            }).catch((er) => {
              that.publishLoading = false
              console.log(er)
            })
          } else {
            pdConAdd({ param }).then(res => {
              if (res.errorCode !== '000000') return
              that.publishLoading = false
              that.shopDia = false
              that.getList()
            }).catch((er) => {
              that.publishLoading = false
              console.log(er)
            })
          }
        } else {
          that.publishLoading = false
          console.log('error submit!!')
          return false
        }
      })
    },
    // 删除
    toDelete (id) {
      this.$confirm(this.translate('delSetPlace'), this.translate('ts'), {
        confirmButtonText: this.translate('qd'),
        cancelButtonText: this.translate('cancel'),
        type: 'warning'
      })
        .then(() => {
          pdConDel({ param: { id } }).then(ress => {
            if (ress.errorCode === '000000') {
              this.getList()
            }
          })
        })
        .catch(() => {
          // this.$message.info(this.translate('canceledDel'))
        })
    }
  }
}
</script>

